<template>
    <div>
        <el-card>
            <el-form class="search" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
                     @submit.native.prevent>
                <el-form-item prop="msgType">
                    <el-select v-model="queryParams.msgType" placeholder="消息类型" clearable size="small"
                               style="width: 120px;"
                               @change="handleQuery">
                        <el-option :value="1" label="通知"/>
                        <el-option :value="2" label="告警"/>
                    </el-select>
                </el-form-item>
                <el-form-item prop="msgTitle">
                    <el-input
                            v-model="queryParams.msgTitle"
                            placeholder="消息标题"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item prop="msgContent">
                    <el-input
                            v-model="queryParams.msgContent"
                            placeholder="消息内容"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item prop="msgIsSolved">
                    <el-select v-model="queryParams.msgIsSolved" placeholder="是否处理" clearable size="small"
                               style="width: 120px;"
                               @change="handleQuery">
                        <el-option :value="1" label="已处理"/>
                        <el-option :value="0" label="未处理"/>
                    </el-select>
                </el-form-item>
                <el-form-item prop="userId">
                    <el-select v-model="queryParams.userId" placeholder="通知人" clearable size="small"
                               style="width: 120px;"
                               @change="handleQuery">
                        <el-option v-for="row in userList" :value="row.userId" :label="row.nickName"/>
                    </el-select>
                </el-form-item>
                <el-form-item prop="dateRange">
                    <el-date-picker clearable size="small"
                                    v-model="queryParams.dateRange"
                                    type="datetimerange"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    range-separator="-"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    style="width: 360px;"
                                    @change="handleQuery">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                            type="primary"
                            icon="el-icon-plus"
                            size="mini"
                            @click="handleAdd"
                            v-hasPermi="['sys:msg:add']"
                    >新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                            type="success"
                            icon="el-icon-edit"
                            size="mini"
                            :disabled="single"
                            @click="handleUpdate"
                            v-hasPermi="['sys:msg:edit']"
                    >修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                            type="danger"
                            icon="el-icon-delete"
                            size="mini"
                            :disabled="multiple"
                            @click="handleDelete"
                            v-hasPermi="['sys:msg:remove']"
                    >删除
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                            type="info"
                            icon="el-icon-edit"
                            size="mini"
                            @click="oneHandle"
                    >一键处理
                    </el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="msgList" border @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"/>
                <el-table-column label="消息类型" align="center" prop="msgType" :formatter="formatMsgType" width="100"/>
                <el-table-column label="消息标题" align="center" prop="msgTitle"/>
                <el-table-column label="消息内容" align="center" prop="msgContent"/>
                <el-table-column label="是否声音提醒" align="center" prop="msgIsSound" width="120">
                    <template slot-scope="{row}">
                        {{row.msgIsSound==1?'是':'否'}}
                    </template>
                </el-table-column>
                <el-table-column label="是否处理" align="center" prop="msgIsSolved" width="100">
                    <template slot-scope="{row}">
                        <el-switch v-model="row.msgIsSolved" :active-value="1" :inactive-value="0"
                                   inactive-color="#ff5353"
                                   @change="handleSolved(row)"/>
                    </template>
                </el-table-column>
                <el-table-column label="通知人" align="center" prop="user.nickName" width="100"/>
                <el-table-column label="消息时间" align="center" prop="msgDt" width="180"/>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleUpdate(scope.row)"
                                v-hasPermi="['sys:msg:edit']"
                        >修改
                        </el-button>
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-delete"
                                @click="handleDelete(scope.row)"
                                v-hasPermi="['sys:msg:remove']"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
            />

            <!-- 添加或修改消息提醒对话框 -->
            <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
                <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                    <el-form-item label="消息类型" prop="msgType">
                        <el-select v-model="form.msgType" placeholder="消息类型" style="width: 100%;">
                            <el-option :value="1" label="通知"/>
                            <el-option :value="2" label="告警"/>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="消息标题" prop="msgTitle">
                        <el-input v-model="form.msgTitle" placeholder="请输入消息标题"/>
                    </el-form-item>
                    <el-form-item label="消息内容" prop="msgContent">
                        <el-input v-model="form.msgContent" type="textarea" placeholder="请输入内容"/>
                    </el-form-item>
                    <el-form-item label="是否声音提醒" prop="msgIsSound">
                        <el-switch v-model="form.msgIsSound" :active-value="1" :inactive-value="0"/>
                    </el-form-item>
                    <el-form-item label="是否处理" prop="msgIsSolved">
                        <el-switch v-model="form.msgIsSolved" :active-value="1" :inactive-value="0"/>
                    </el-form-item>
                    <el-form-item label="通知人" prop="userId">
                        <el-select v-model="form.userId" placeholder="通知人" style="width: 100%;">
                            <el-option v-for="row in userList" :value="row.userId" :label="row.nickName"/>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="消息时间" prop="msgDt">
                        <el-date-picker style="width: 100%"
                                        v-model="form.msgDt"
                                        type="datetime"
                                        value-format="yyyy-MM-dd HH:mm:ss"
                                        placeholder="选择消息时间">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
    import {pageRemind, getRemind, delRemind, addRemind, updateRemind, oneHandle} from '@/api/app/msg/remind'
    import {listUser} from '@/api/system/user'

    export default {
        name: 'Remind',
        components: {},
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 消息提醒表格数据
                msgList: [],
                // 弹出层标题
                title: '',
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    userId: null,
                    msgType: null,
                    msgTitle: null,
                    msgContent: null,
                    msgIsSolved: null,
                    dateRange: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    msgType: [
                        {required: true, message: '消息类型不能为空', trigger: 'blur'}
                    ],
                    msgTitle: [
                        {required: true, message: '消息标题不能为空', trigger: 'blur'}
                    ],
                    msgContent: [
                        {required: true, message: '消息内容不能为空', trigger: 'blur'}
                    ],
                    msgIsSound: [
                        {required: true, message: '是否声音提醒不能为空', trigger: 'blur'}
                    ],
                    msgIsSolved: [
                        {required: true, message: '是否处理不能为空', trigger: 'blur'}
                    ],
                    userId: [
                        {required: true, message: '通知人不能为空', trigger: 'blur'}
                    ],
                    msgDt: [
                        {required: true, message: '消息时间不能为空', trigger: 'blur'}
                    ]
                },
                userList: []
            }
        },
        created() {
            this.getUserList()
            this.getList()
        },
        methods: {
            getUserList() {
                listUser({}).then(res => {
                    this.userList = res.data.rows
                })
            },
            /** 查询消息提醒列表 */
            getList() {
                let data = this.formatDateRange(this.queryParams, false)
                this.loading = true
                pageRemind(data).then(res => {
                    this.msgList = res.data.rows
                    this.total = res.data.total
                    this.loading = false
                })
            },
            // 取消按钮
            cancel() {
                this.open = false
                this.reset()
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    userId: null,
                    msgType: null,
                    msgTitle: null,
                    msgContent: null,
                    msgIsSound: null,
                    msgIsSolved: null,
                    msgDt: null
                }
                this.resetForm('form')
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1
                this.getList()
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm('queryForm')
                this.handleQuery()
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset()
                this.open = true
                this.title = '添加消息提醒'
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset()
                const id = row.id || this.ids
                getRemind(id).then(res => {
                    this.form = res.data
                    this.open = true
                    this.title = '修改消息提醒'
                })
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateRemind(this.form).then(res => {
                                this.msgSuccess('修改成功')
                                this.open = false
                                this.getList()
                            })
                        } else {
                            addRemind(this.form).then(res => {
                                this.msgSuccess('新增成功')
                                this.open = false
                                this.getList()
                            })
                        }
                    }
                })
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids
                this.$confirm('是否确认删除?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delRemind(ids)
                }).then(() => {
                    this.getList()
                    this.msgSuccess('删除成功')
                })
            },
            oneHandle() {
                oneHandle({}).then(res => {
                    this.msgSuccess('处理成功')
                    this.getList()
                })
            },
            handleSolved(row) {
                updateRemind(row).then(res => {
                    this.getList()
                })
            },
            formatMsgType(row) {
                if (row.msgType == 1) return '通知'
                if (row.msgType == 2) return '告警'
            }
        }
    }
</script>
<style lang="scss" scoped>
</style>
